package com.jiu345.api.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiu345.api.mapper.BannerMapper;
import com.jiu345.api.mapper.CartMapper;
import com.jiu345.api.mapper.GoodsSkusMapper;
import com.jiu345.api.mapper.SkusSpecMapper;
import com.jiu345.api.pojo.Banner;
import com.jiu345.api.pojo.Cart;
import com.jiu345.api.pojo.GoodsSkus;
import com.jiu345.api.pojo.vo.CartVo;
import com.jiu345.api.service.IBannerService;
import com.jiu345.api.service.ICartService;
import com.jiu345.core.page.PageQuery;
import com.jiu345.core.page.TableDataInfo;
import com.jiu345.users.pojo.bo.CartBo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.dromara.common.core.domain.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * package:IGoodsCateServiceImpl
 * @author MrLei QQ:235921049
 * @date 2024-07-19
 * desc:购物车
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ICartServiceImpl implements ICartService {
    private final CartMapper baseMapper;
//    private final GoodsSkusMapper skusMapper;

    /*根据用户ID查询购物车列表*/
    @Override
    public TableDataInfo<CartVo> selectByUserIdPageList(PageQuery pageQuery) {
        //获取登录用户
        Long userId = StpUtil.getLoginIdAsLong();
        //查询分页
        Page<CartVo> result = baseMapper.selectPageList(pageQuery.build(),userId);
        return TableDataInfo.build(result);
    }
    /*添加购物车*/
    @Override
    public Boolean insertByBo(Cart bo) {
        //设置登录用户ID和创建时间
        bo.setUserId(Long.valueOf(StpUtil.getLoginIdAsString()));
        bo.setCreateTime(new Date());
        bo.setUpdateTime(new Date());
        Long userId = Long.valueOf(StpUtil.getLoginId().toString());
//        Long skuId = bo.getSkuId();
//
//        Cart cart = baseMapper.selectBySkuId(userId, skuId);
//        if(cart != null) {
//            cart.setCount(cart.getCount() + bo.getCount());
//            int updateResult = baseMapper.updateById(cart);
//            return updateResult > 0;
//        }else{
//            // 无值则执行添加
//            int insertResult = baseMapper.insert(bo);
//            return insertResult > 0; // 如果插入成功，则返回true
//        }

        // 根据用户ID查询和skuId查询购物车列表,与上面的方法等效
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<Cart>()
            .eq(Cart::getUserId, userId)
            .eq(Cart::getSkuId, bo.getSkuId());

        Cart cart = baseMapper.selectOne(lqw);
        if(cart != null){
            //如果不为空，则只累加count
            cart.setCount(cart.getCount() + bo.getCount());
            int updateResult = baseMapper.updateById(cart);
            return updateResult > 0; // 如果更新成功，则返回true
        }else{
            // 无值则执行添加
            int insertResult = baseMapper.insert(bo);
            return insertResult > 0; // 如果插入成功，则返回true
        }
    }

    /* 删除购物车商品*/
    public Boolean deleteSkuGoodsByIds(Collection<String> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }

    /*修改购物车商品数量*/
    public Boolean updateNumber(CartBo bo) {
        Cart cart = new Cart();
        cart.setId(bo.getId());
        cart.setCount(bo.getCount());
        cart.setSelected(bo.getSelected());
        return baseMapper.updateById(cart)> 0;
    }

    /*购物车全选/取消*/
    public Boolean isCheckedAll(CartBo bo) {
        //1、查询当前用户
        Long userId = StpUtil.getLoginIdAsLong();
        //2、根据前端请求的false/true，对所有列表的值进行修改
        return baseMapper.isCheckedAll(bo,userId);
    }



}
